﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using HRM.Models;
using System.IO;

namespace HRM.Controllers
{
    public class RestoreController : Controller
    {
        //
        // GET: /Restore/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Restore(String filePath)
        {            
            var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
            var obj = new WebData("").Users.Restore(filePath,"NS");
            var jsonResult = new ContentResult
            {
                Content = JsonConvert.SerializeObject(obj, settings),
                ContentType = "application/json"
            };
            return jsonResult;
        }

        public ActionResult GetListBackup() {
            CKey item;
            List<CKey> list = new List<CKey>();
            String backupDIR = "backup";
            var filePath = System.Web.HttpContext.Current.Server.MapPath("~/" + backupDIR);
            if (!System.IO.Directory.Exists(filePath))
            {
                System.IO.Directory.CreateDirectory(filePath);
            }

            DirectoryInfo _infos = new DirectoryInfo(filePath);
            FileInfo[] files = _infos.GetFiles();
            foreach (FileInfo _info in files) {
                item = new CKey();
                item.key = _info.Name;
                item.value = _info.FullName;
                list.Insert(0, item);
            }

            var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };            
            var jsonResult = new ContentResult
            {
                Content = JsonConvert.SerializeObject(list, settings),
                ContentType = "application/json"
            };
            return jsonResult;
        }

    }
}
